//
//  ShowLargeImageViewController.m
//  testImage
//
//  Created by lianglibao on 2019/2/25.
//  Copyright © 2019年 lianglibao. All rights reserved.
//

#import "ShowLargeImageViewController.h"
#import "ATRAZ500MapCollectionAction.h"
#import "ATRAZMap_CollectionAction.h"
#import "ATRAZMap_DataLoader.h"
#import "ATRAZMapDataLoader.h"
#import "UIImage+Test.h"
#import "ATUtil.h"

@interface ShowLargeImageViewController ()<ATRAZ500MapCollectionActionDelegate>
@property (nonatomic, strong) ATRAZ500MapCollectionAction *raz500CollectionAction;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) ATRAZMap_CollectionAction *RAZ500_Map;

@end


@implementation ShowLargeImageViewController

- (ATRAZ500MapCollectionAction *)raz500CollectionAction {
    if (!_raz500CollectionAction) {
        _raz500CollectionAction = [ATRAZ500MapCollectionAction new];
        _raz500CollectionAction.delegate = self;
        
        _raz500CollectionAction.dataSource = self.dataSource;
        _raz500CollectionAction.collectionView.frame = CGRectMake(0,
                                                                  0,
                                                                  ATSCREEN_WIDTH(),
                                                                  ATSCREEN_HEIGHT()
                                                                  );
        if (@available(iOS 11.0, *)) {
            [self.raz500CollectionAction.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
            [self.raz500CollectionAction.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
    }
    return _raz500CollectionAction;
}

- (ATRAZMap_CollectionAction *)RAZ500_Map {
    if (!_RAZ500_Map) {
        _RAZ500_Map = [ATRAZMap_CollectionAction new];
        _RAZ500_Map.dataSource = self.dataSource;
        _RAZ500_Map.collectionView.frame = CGRectMake(0, 0, ATSCREEN_WIDTH(), ATSCREEN_HEIGHT());
        if (@available(iOS 11.0, *)) {
            [self.RAZ500_Map.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
            [self.RAZ500_Map.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
    }
    return _RAZ500_Map;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSStringFromClass(self.class);
    
    self.queue = dispatch_get_global_queue(0, 0 );
    
    NSString *deviceString;
    if ([ATUtil isiPhoneXR]) {
        deviceString = @"iPhoneXR";
    } else if ([ATUtil isiPhoneX]) {
        deviceString = @"iPhoneX";
    } else if ([ATUtil isiPhoneSize_5_5]) {
        deviceString = @"iPhone5_5";
    } else if ([ATUtil isiPhoneSize_4_7]) {
        deviceString = @"iPhone4_7";
    } else if ([ATUtil isiPhoneSize_4_0]) {
        deviceString = @"iPhone4_0";
    } else if ([ATUtil isiPhoneSize_3_5]) {
        deviceString = @"iPhone3_5";
    } else {
        deviceString = @"iPad";
    }
    
    if (self.loadLargeType == kLoadLargeImageTypeUseTileLayer) {
        // 方式一: 在线裁剪,使用CATileLayer渲染.
        [self loadLargeMapImageTypeOne:deviceString];
        
    } else if (self.loadLargeType == kLoadLargeImageTypeUseClipLargeImage) {
        // 方式二: 在线裁剪,使用切割长图方式加载.
        [self loadLargeMapImageTypeTwo:deviceString];
    }
}

// MARK: - 方式一: 在线裁剪,使用CATileLayer渲染.
- (void)loadLargeMapImageTypeOne:(NSString *)deviceString {
    NSString *basePath = $(@"%@/maps/%@/", [ATUtil getCachePath], deviceString);
    
    // 将iPad数据处理成适配当前设备的数据
    if (![ATUtil isiPad]) {
        [self p_handleMapsDataFromPadToPhone:deviceString basePath:basePath];
    }
    
    dispatch_async(self.queue, ^{
        // self.dataSource = [ATRAZMapDataLoader shareInstance].loadRazMapData;
        self.dataSource = [ATRAZMap_DataLoader shareInstance].loadRazMapData;
        if (self.dataSource.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.view addSubview:self.RAZ500_Map.collectionView];
            });
        }
    });
}

// MARK: - 方式二: 在线裁剪,使用切割长图方式加载.
- (void)loadLargeMapImageTypeTwo:(NSString *)deviceString {
    NSString *basePath = $(@"%@/RAZ_Maps/%@/", [ATUtil getCachePath], deviceString);
    // 将iPad图片裁剪为适配当前屏幕的图片
    [self p_handleImageFromPadToPhone:deviceString basePath:basePath];
    
    // 将背景图片裁剪成单个屏幕图片.
    [self p_clipBgToScreenSizeWithBasePath:basePath deviceString:deviceString];
    
    // 将iPad关卡锚点数据适配为当前屏幕m关卡锚点数据.
    [self p_handleCardPointFromPadToPhone:deviceString basePath:basePath];
    
    dispatch_async(self.queue, ^{
        self.dataSource = [ATRAZMapDataLoader shareInstance].loadRazMapData;
        if (self.dataSource.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.view addSubview:self.raz500CollectionAction.collectionView];
            });
        }
    });
}

// MARK: - ATRAZ500MapCollectionActionDelegate
- (void)razMapCollectionActionWithClickedButton:(UIButton *)button
                                        atIndex:(NSInteger)index {
    
}

- (void)p_clipBgToScreenSizeWithBasePath:(NSString *)basePath
                            deviceString:(NSString *)deviceString  {
    __weak typeof(self) weakSelf = self;
    dispatch_async(self.queue, ^{
        NSString *tmpImageCachePath =  $(@"%@%@_subject_one_bg_0.png",
                                         basePath,
                                         deviceString
                                         );
        
        if ([ATUtil fileIsExistOfPath:tmpImageCachePath]) {
            return ;
        }
        NSString *imageCachePath = $(@"%@%@_subject_one_bg.png",
                                     basePath,
                                     deviceString
                                     );
        UIImage *tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_one_bg"];
        
        imageCachePath = $(@"%@%@_subject_two_bg.png",
                           basePath,
                           deviceString
                           );
        tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_two_bg"];
        
        imageCachePath = $(@"%@%@_subject_three_bg.png",
                           basePath,
                           deviceString);
        tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_three_bg"];
        
        imageCachePath = $(@"%@%@_subject_four_bg.png",
                           basePath,
                           deviceString);
        tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_four_bg"];
        
        imageCachePath = $(@"%@%@_subject_five_bg.png",
                           basePath,
                           deviceString);
        tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_five_bg"];
        
        imageCachePath = $(@"%@%@_subject_six_bg.png",
                           basePath,
                           deviceString);
        tempImage = [UIImage imageWithContentsOfFile:imageCachePath];
        [weakSelf p_clipImageWithImage:tempImage
                              basePath:basePath
                          deviceString:deviceString
                             imageName:@"_subject_six_bg"];
        
    });
}

- (NSArray *)p_clipImageWithImage:(UIImage *)sourceImage
                         basePath:(NSString *)basePath
                     deviceString:(NSString *)deviceString
                        imageName:(NSString *)imageName {
    CGFloat screenWith = [UIScreen mainScreen].bounds.size.width;
    int count = sourceImage.size.width / screenWith;
    if (sourceImage.size.width - screenWith * count) {
        count += 1;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        UIImage *newImage = [sourceImage cutImageWithIndex:i];
        
        if (newImage) {
            NSString *cachePath = $(@"%@%@%@_%d.png",
                                    basePath,
                                    deviceString,
                                    imageName,
                                    i
                                    );
            newImage = [UIImage imageWithCGImage:newImage.CGImage
                                           scale:[UIScreen mainScreen].scale
                                     orientation:UIImageOrientationUp];
            
            NSData *imageData = [UIImage imageDataWithImage:newImage];
            [ATUtil saveFile:cachePath withData:imageData];
            [tempArray addObject:newImage];
        }
    }
    
    return tempArray;
}

// MARK: - 将iPad地图数据计算出适配到各iPhone设备
- (void)p_handleImageFromPadToPhone:(NSString *)deviceString
                           basePath:(NSString *)basePath {
    // 处理背景图片.
    NSString *tmpName = @"_subject_one_bg.png";
    NSString *targetDataSavePath;
    @autoreleasepool {
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);
        if ([ATUtil fileIsExistOfPath:targetDataSavePath]) {
            return;
        }
        
        NSString *imagePath = [ATUtil at_resourcePathWithClass:self.class
                                                    bundleName:@"maps"
                                                       subPath:@"iPad"
                                                      fileName:$(@"iPad%@", tmpName)];
        
        UIImage *targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        NSData *targetImageData = [UIImage imageDataWithImage:targetImage];
        
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        tmpName = @"_subject_two_bg.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        tmpName = @"_subject_three_bg.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        tmpName = @"_subject_four_bg.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        tmpName = @"_subject_five_bg.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        tmpName = @"_subject_six_bg.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 处理关卡图片.
        // 主题1 card Dont image
        tmpName = @"_normal_card_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题1 card Done image
        tmpName = @"_normal_card_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题1 card cardSuperDoneImage
        tmpName = @"_super_card_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题1 card cardSuperDontImage
        tmpName = @"_super_card_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题1 stationImage
        tmpName = @"_whirligig_station.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题1 水中关卡图片(可点击)
        tmpName = @"_water_card_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        
        // 主题1 水中关卡图片(不可可点击)
        tmpName = @"_water_card_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题2 card Dont image
        tmpName = @"_subject_two_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题2 card Done image
        tmpName = @"_subject_two_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题2 card cardSuperDontImage
        tmpName = @"_subject_two_super_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题2 card cardSuperDoneImage
        tmpName = @"_subject_two_super_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题3 card Dont image
        tmpName = @"_subject_three_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题3 card Done image
        tmpName = @"_subject_three_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题3 card cardSuperDontImage
        tmpName = @"_subject_three_super_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题3 card cardSuperDoneImage
        tmpName = @"_subject_three_super_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题4 card Dont image
        tmpName = @"_subject_four_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题4 card Done image
        tmpName = @"_subject_four_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题4 card cardSuperDontImage
        tmpName = @"_subject_four_super_dont.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
        
        // 主题4 card cardSuperDoneImage
        tmpName = @"_subject_four_super_done.png";
        imagePath = [ATUtil at_resourcePathWithClass:self.class
                                          bundleName:@"maps"
                                             subPath:@"iPad"
                                            fileName:$(@"iPad%@", tmpName)];
        targetImage = [[UIImage imageWithContentsOfFile:imagePath] raz_scaleImageToFitScreenSize];
        targetImageData = [UIImage imageDataWithImage:targetImage];
        targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
        [ATUtil saveFile:targetDataSavePath withData:targetImageData];
    }
}

- (void)p_handleMapsDataFromPadToPhone:(NSString *)deviceString
                              basePath:(NSString *)basePath {
    // 处理背景图片和关卡图片.
    [self p_handleImageFromPadToPhone:deviceString basePath:basePath];
    
    // 处理关卡锚点数据.
    [self p_handleCardPointFromPadToPhone:deviceString basePath:basePath];
}

- (void)p_handleCardPointFromPadToPhone:(NSString *)deviceString
                               basePath:(NSString *)basePath {
    // 处理关卡锚点数据.
    @autoreleasepool {
        // 处理主题1 card point
        NSString *tmpName = @"_subject_one_card_point.json";
        NSString *targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);
        if ([ATUtil fileIsExistOfPath:targetDataSavePath]) {
            return ;
        }
        NSString *cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                                        bundleName:@"maps"
                                                           subPath:@"iPad"
                                                          fileName:$(@"iPad%@", tmpName)];
        NSData *cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        NSArray *cardPointArry = nil;
        NSMutableArray *targetCardPoint = [NSMutableArray array];
        NSData *targetCardPointData = nil;
        NSError *error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        // 处理主题2 card point
        tmpName = @"_subject_two_card_point.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        // 处理主题3 card point
        tmpName = @"_subject_three_card_point.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        // 处理主题4 card point
        tmpName = @"_subject_four_card_point.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        // 处理主题5 card point
        tmpName = @"_subject_five_card_point.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        // 处理主题6 card point
        tmpName = @"_subject_six_card_point.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
        
        
        tmpName = @"_IP_MovePoints.json";
        cardPointpath = [ATUtil at_resourcePathWithClass:self.class
                                              bundleName:@"maps"
                                                 subPath:@"iPad"
                                                fileName:$(@"iPad%@", tmpName)];
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        cardPointArry = nil;
        [targetCardPoint removeAllObjects];
        error = nil;
        if (cardPointData) {
            cardPointArry = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                            options:0
                                                              error:&error];
        }
        if (error) {
            NSLog(@"%@", error);
        } else {
            [cardPointArry enumerateObjectsUsingBlock:^(NSString *obj,
                                                        NSUInteger idx,
                                                        BOOL * _Nonnull stop) {
                CGRect rect = CGRectFromString(obj);
                rect.size.width = rect.size.width/ATSCREEN_SCALE_RATIO();
                rect.size.height = rect.size.height/ATSCREEN_SCALE_RATIO();
                rect.origin.x = rect.origin.x/ATSCREEN_SCALE_RATIO();
                rect.origin.y = rect.origin.y/ATSCREEN_SCALE_RATIO();
                NSString *targetRect = NSStringFromCGRect(rect);
                [targetCardPoint addObject:targetRect];
            }];
            targetCardPointData = [NSJSONSerialization dataWithJSONObject:targetCardPoint
                                                                  options:NSJSONWritingPrettyPrinted
                                                                    error:&error];
            targetDataSavePath = $(@"%@%@%@", basePath, deviceString, tmpName);;
            [ATUtil saveFile:targetDataSavePath withData:targetCardPointData];
        }
    }
    
}

- (void)dealloc {
    if (self.loadLargeType == kLoadLargeImageTypeUseTileLayer) {
        [[ATRAZMap_DataLoader shareInstance] clearCache];
    } else if (self.loadLargeType == kLoadLargeImageTypeUseClipLargeImage) {
        [[ATRAZMapDataLoader shareInstance] clearCache];
    }
}

@end

